﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SchemeManager
{
    /// <summary>
    /// 用户信息
    /// 储存在外部数据格式为username+magicstring+password
    /// </summary>
    class User
    {
        /// <summary>
        /// 用户数据文件
        /// </summary>
        private static readonly string USER_FILE = new FileInfo(typeof(User).Assembly.Location).DirectoryName + "\\Datas\\User.dat";
        /// <summary>
        /// 神奇的字符串，用来分割 用户名与密码
        /// </summary>
        private const string MAGIC_STRING = "!@#@#!@#@##@!#!@#@#!#!@#@!#@!$#$#$##";

        /// <summary>
        /// 用户名称
        /// </summary>
        public string Name
        {
            get;
            set;
        }

        /// <summary>
        /// 用户密码
        /// </summary>
        public string Password
        {
            get;
            set;
        }

        /// <summary>
        /// 是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsEmpty()
        {
            return string.IsNullOrEmpty(Name);
        }

        /// <summary>
        /// 验证用户名与密码是否相同
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Validate(string name, string password)
        {
            return Name.Equals(name) && Password.Equals(password);
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <returns></returns>
        public static User LoadUser()
        {
            try
            {
                if (File.Exists(USER_FILE) == false)
                    return null;
                User user = new User();
                byte[] enBytes = File.ReadAllBytes(USER_FILE);
                if (enBytes.Length == 0) //文件为空，则表示还没有储存数据
                {
                    return user;
                }
                byte[] plainBytes = Comm.Crypt.SchemeCrypt.Instance.Decrypt(enBytes);
                string str = System.Text.Encoding.Unicode.GetString(plainBytes);
                string[] ss = str.Split(new string[] { MAGIC_STRING }, StringSplitOptions.RemoveEmptyEntries);
                user.Name = ss[0];
                user.Password = ss[1];

                return user;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 保存用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool SaveUser(User user)
        {
            try
            {
                string str = user.Name + MAGIC_STRING + user.Password;

                byte[] plainBytes = System.Text.Encoding.Unicode.GetBytes(str);
                byte[] enBytes = Comm.Crypt.SchemeCrypt.Instance.Encrypt(plainBytes);
                File.WriteAllBytes(USER_FILE, enBytes);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
